<?php

namespace common\widgets\grid;

use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;

/**
 * PortletColumn displays a column of mark text content in a grid view.
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class PortletColumn extends \yii\grid\DataColumn {

    public $format = 'raw';

    /**
     * @var string|null 缩略图
     */
    public $image = null;

    /**
     * @var string 描述
     */
    public $description = null;

    /**
     * Returns the data cell value.
     * @param mixed $model the data model
     * @param mixed $key the key associated with the data model
     * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
     * @return string the data cell value
     */
    public function getDataCellValue($model, $key, $index) {
        $title = parent::getDataCellValue($model, $key, $index);
        if (!$title) {
            return $title;
        }
        $image = null;
        $description = null;
        if ($this->image !== false) {
            if ($this->image) {
                $image = ArrayHelper::getValue($model, $this->image);
            }
            $image = Yii::$app->formatter->asImage($image, []);
        }
        if ($this->description) {
            $description = ArrayHelper::getValue($model, $this->description);
        }
        return $this->renderPortletCellContent($title, $image, $description);
    }

    protected function renderPortletCellContent($title, $image, $description) {
        $image = $image ? Html::tag('div', $image, ['class' => 'mediabox-thumb']) : '';
        $description = $description ? Html::tag('small', $description) : '';
        $title = Html::tag('h2', $title . $description, ['class' => 'mediabox-text']);
        return Html::tag('div', $image . $title, ['class' => 'mediabox']);
    }

}
